﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TableParsers
{
    public class ResultsTableEntry
    {
        public string Course;        
        public bool IsUnit;
        public bool IsLevel { get { return !IsUnit; } set { IsUnit = !value; } }
        public int UnitOrLevel;
        public int? Grammar;
        public int? Vocabulary;
        public int? Listening;
        public int? Reading;
        public int Avarage;
    }

    public class ResultsTable: List<ResultsTableEntry>
    {
        public static ResultsTable Fetch(WebLayer.LoginToken loginToken)
        {
            WebPageParser.WebPageCache pageCache = new WebPageParser.WebPageCache();
            pageCache.LoginToken = loginToken;

            return Fetch(pageCache);
        }

        static int? ParseResultPercents(string strResult)
        {
            strResult = strResult.Replace("%", "").Trim();
            if (strResult.Length == 0) return null;

            int res = 0;

            if (Int32.TryParse(strResult, out res) == false)
            {
                return null;
            }

            return res;
        }

        public static ResultsTable Fetch(WebPageParser.WebPageCache pageCache)
        {
            WebPageParser.ResultsWebPage webPage = pageCache.GetPage<WebPageParser.ResultsWebPage>();

            ResultsTable resultsTable = new ResultsTable();
            foreach (WebPageParser.ResultsTableEntry webEntry in webPage.TableContent)
            {
                ResultsTableEntry resultEntry = new ResultsTableEntry();

                resultEntry.Course = webEntry.Course.Trim();

                string unit = webEntry.Unit.ToLower();
                if (unit.Contains("unit"))
                {
                    unit = unit.Replace("unit", "").Trim();
                    resultEntry.IsUnit = true;

                    if (Int32.TryParse(unit, out resultEntry.UnitOrLevel) == false)
                    {
                        resultEntry.UnitOrLevel = 0;
                    }                    
                }
                else if (unit.Contains("level"))
                {
                    string level = unit.Replace("level", "").Trim();
                    resultEntry.IsLevel = true;

                    if (Int32.TryParse(level, out resultEntry.UnitOrLevel) == false)
                    {
                        resultEntry.UnitOrLevel = 0;
                    }                    
                }
                else
                {
                    throw new FormatException("Level or Unit field is not Level or Unit");
                }

                resultEntry.Grammar = ParseResultPercents(webEntry.Grammar);
                resultEntry.Vocabulary = ParseResultPercents(webEntry.Vocabulary);
                resultEntry.Listening = ParseResultPercents(webEntry.Listening);
                resultEntry.Reading = ParseResultPercents(webEntry.Reading);
                resultEntry.Avarage = ParseResultPercents(webEntry.Avarage)??0;

                resultsTable.Add(resultEntry);
            }

            return resultsTable;
        }
    }
}
